package com.example.fengyungo.utils;

import static android.content.Context.MODE_PRIVATE;

import android.content.SharedPreferences;
import android.app.Service;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;

import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.MessageConstant;
import com.example.fengyungo.constant.OrderConstant;
import com.example.fengyungo.constant.ResultConstant;
import com.example.fengyungo.domain.Driver;
import com.example.fengyungo.domain.Message;
import com.example.fengyungo.domain.MessageList;
import com.example.fengyungo.domain.Order;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 一些工具类
 *
 * @author XLR
 */
public class DriverUtils {

    private static final String TAG = "DriverUtils";

    /**
     * 处理来自后端的Order响应数据，结果存到List中
     *
     * @param jsonData String
     * @return List<MessageList>
     */
    public static List<Order> parseOrdersJsonWithJsonObject(String jsonData) {
        ArrayList<Order> orders = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            JSONArray jsonArray = jsonObject.getJSONArray(ResultConstant.DATA);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject orderJson = jsonArray.getJSONObject(i);
                Order order = new Order();

                String userId = orderJson.getString(OrderConstant.USER_ID);
                String receiverId = orderJson.getString(OrderConstant.RECEIVER_ID);
                String number = orderJson.getString(OrderConstant.NUMBER);
                String begin = orderJson.getString(OrderConstant.BEGIN);
                String end = orderJson.getString(OrderConstant.END);
                String earliestTime = orderJson.getString(OrderConstant.EARLIEST_TIME);
                String latestTime = orderJson.getString(OrderConstant.LATEST_TIME);
                String note = orderJson.getString(OrderConstant.NOTE);
                String price = orderJson.getString(OrderConstant.PRICE);
                String payStatus = orderJson.getString(OrderConstant.PAY_STATUS);
                String orderStatus = orderJson.getString(OrderConstant.ORDER_STATUS);
                String credit = orderJson.getString(OrderConstant.CREDIT);

                order.setUserId(userId);
                order.setReceiverId(receiverId);
                order.setNumber(number);
                order.setBegin(begin);
                order.setEnd(end);
                order.setEarliestTime(earliestTime);
                order.setLatestTime(latestTime);
                order.setNote(note);
                order.setPrice(Double.parseDouble(price));
                order.setPayStatus(Integer.parseInt(payStatus));
                order.setOrderStatus(Integer.parseInt(orderStatus));
                order.setCredit(Float.parseFloat(credit));

                orders.add(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orders;
    }

    /**
     * 处理来自后端的Driver响应数据，结果存到List中
     *
     * @param jsonData String
     * @return List<MessageList>
     */
    public static List<Driver> parseDriversJsonWithJsonObject(String jsonData) {
        ArrayList<Driver> drivers = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            JSONArray jsonArray = jsonObject.getJSONArray(ResultConstant.DATA);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject driverJson = jsonArray.getJSONObject(i);
                Driver driver = new Driver();

                String id = driverJson.getString(DriverConstant.ID);
                String username = driverJson.getString(DriverConstant.USERNAME);
                String sex = driverJson.getString(DriverConstant.SEX);
                String phone = driverJson.getString(DriverConstant.PHONE);
                String password = driverJson.getString(DriverConstant.PASSWORD);
                String idCard = driverJson.getString(DriverConstant.ID_CARD);
                String avatar = driverJson.getString(DriverConstant.AVATAR);
                String status = driverJson.getString(DriverConstant.STATUS);
                String createTime = driverJson.getString(DriverConstant.CREATE_TIME);
                String nickname = driverJson.getString(DriverConstant.NICKNAME);
                String mail = driverJson.getString(DriverConstant.MAIL);

                driver.setId(id);
                driver.setUsername(username);
                driver.setSex(sex);
                driver.setPhone(phone);
                driver.setPassword(password);
                driver.setIdCard(idCard);
                driver.setAvatar(avatar);
                driver.setStatus(Integer.parseInt(status));
                driver.setCreateTime(createTime);
                driver.setNickname(nickname);
                driver.setMail(mail);

                drivers.add(driver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drivers;
    }

    /**
     * 处理来自后端的MessageList响应数据，结果存到List中
     *
     * @param jsonData String
     * @return List<MessageList>
     */
    public static List<MessageList> parseMessageListJsonWithJsonObject(String jsonData) {
        List<MessageList> messageLists = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            JSONArray jsonArray = jsonObject.getJSONArray(ResultConstant.DATA);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject messageListJson = jsonArray.getJSONObject(i);
                MessageList messageList = new MessageList();

                String listId = messageListJson.getString(MessageConstant.LIST_ID);
                String driverId = messageListJson.getString(MessageConstant.DRIVER_ID);
                String userId = messageListJson.getString(MessageConstant.USER_ID);
                String userNickname = messageListJson.getString(MessageConstant.USER_NICKNAME);
                String userAvatar = messageListJson.getString(MessageConstant.USER_AVATAR);
                int userOnline = messageListJson.getInt(MessageConstant.USER_ONLINE);
                int driverUnread = messageListJson.getInt(MessageConstant.DRIVER_UNREAD);
                String lastContent = messageListJson.getString(MessageConstant.LAST_CONTENT);
                String lastTime = messageListJson.getString(MessageConstant.LAST_TIME);
                int driverListStatus = messageListJson.getInt(MessageConstant.DRIVER_LIST_STATUS);

                messageList.setListId(listId);
                messageList.setDriverId(driverId);
                messageList.setUserId(userId);
                messageList.setUserNickname(userNickname);
                messageList.setUserAvatar(userAvatar);
                messageList.setUserOnline(userOnline);
                messageList.setDriverUnread(driverUnread);
                messageList.setLastContent(lastContent);
                messageList.setLastTime(lastTime.substring(5, 10));
                messageList.setDriverListStatus(driverListStatus);
                Log.d(TAG, "parseMessageListJsonWithJsonObject: messageList="+messageList);

                messageLists.add(messageList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return messageLists;
    }

    /**
     * 处理来自后端的Message响应数据，结果存到List中
     *
     * @param jsonData String
     * @return List<Message>
     */
    public static List<Message> parseMessageJsonWithJsonObject(String jsonData) {
        List<Message> messages = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            JSONArray jsonArray = jsonObject.getJSONArray(ResultConstant.DATA);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject messageJson = jsonArray.getJSONObject(i);
                Message message = new Message();

                String messageId = messageJson.getString(MessageConstant.MESSAGE_ID);
                String senderId = messageJson.getString(MessageConstant.SENDER_ID);
                String receiverId = messageJson.getString(MessageConstant.RECEIVER_ID);
                String content = messageJson.getString(MessageConstant.CONTENT);
                String sendTime = messageJson.getString(MessageConstant.SEND_TIME);

                message.setMessageId(messageId);
                message.setSenderId(senderId);
                message.setReceiverId(receiverId);
                message.setContent(content);
                message.setSendTime(sendTime);

                messages.add(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return messages;
    }

}
